﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using AutoLoL.Entities;
using AutoLoL.Shared;
using ImageConverter = AutoLoL.Logic.Utils.ImageConverter;

namespace AutoLoL.Logic
{
    public class MasteryRepository
    {
        private static MasteryEditor _masteryEditor;

        public static MasteryEditor GetEditor(bool cached = true)
        {
            if (_masteryEditor == null || !cached)
            {
                MasteryEditor masteryEditor = new MasteryEditor();
                masteryEditor.MasteryTrees = new List<MasteryTree>();

                if (File.Exists(Constants.MASTERIES_XML_FILE))
                {
                    XDocument masteriesDoc = XDocument.Load(Constants.MASTERIES_XML_FILE);
                    foreach (XElement treeElement in masteriesDoc.Root.Elements("tree"))
                    {
                        MasteryTree masteryTree = new MasteryTree();
                        masteryTree.Name = treeElement.Attribute("name").Value;
                        masteryTree.Masteries = new List<Mastery>();

                        Dictionary<Mastery, int> prerequisiteMapping = new Dictionary<Mastery, int>();

                        int row = 0;
                        foreach (XElement rowElement in treeElement.Elements("row"))
                        {
                            int pointsToActivate = int.Parse(rowElement.Attribute("required").Value);

                            foreach (XElement masteryElement in rowElement.Elements("mastery"))
                            {
                                int id = int.Parse(masteryElement.Element("id").Value);
                                int max = int.Parse(masteryElement.Element("max").Value);
                                int column = int.Parse(masteryElement.Element("column").Value);
                                string name = masteryElement.Element("name").Value;
                                string icon = masteryElement.Element("icon").Value;

                                XElement descriptionElement = masteryElement.Element("descriptions");
                                XElement[] descriptionElements = descriptionElement.Elements("description").ToArray();
                                string[] descriptions = new string[descriptionElements.Length];
                                for (int i = 0; i < descriptionElements.Length; i++)
                                {
                                    descriptions[i] = descriptionElements[i].Attribute("description").Value;
                                }

                                Mastery mastery = new Mastery();
                                mastery.MasteryId = id;
                                mastery.MaximumRank = max;
                                mastery.PointsToActivate = pointsToActivate;
                                mastery.Row = row;
                                mastery.Column = column;
                                mastery.Name = name;
                                mastery.ParentTree = masteryTree;
                                mastery.Descriptions = descriptions;

                                mastery.IconBitmapImage = ImageConverter.ConvertFileToBitmapImage(Path.Combine(Constants.MASTERIES_DIRECTORY, icon));
                                mastery.IconBitmapImageDisabled = ImageConverter.ConvertBitmapImageToGreyScale(mastery.IconBitmapImage);

                                if (masteryElement.Element("prereq") != null)
                                {
                                    prerequisiteMapping.Add(mastery, int.Parse(masteryElement.Element("prereq").Value));
                                }

                                masteryTree.Masteries.Add(mastery);
                            }

                            row++;
                        }

                        foreach (Mastery mastery in prerequisiteMapping.Keys)
                        {
                            int prerequisiteId = prerequisiteMapping[mastery];
                            Mastery prerequisiteMastery = masteryTree.Masteries.Where(c => c.MasteryId == prerequisiteId).Single();
                            mastery.Prerequisite = prerequisiteMastery;
                        }

                        masteryEditor.MasteryTrees.Add(masteryTree);
                    }
                }

                _masteryEditor = masteryEditor;
            }

            return _masteryEditor;
        }
    }
}
